package com.coke.demo.mapper;

import com.coke.demo.model.Record;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19602
 */
@Component
public interface RecordMapper {

    /**
     * 查询所有预约记录
     *
     * @return List<Record>
     */
    @Select("select * from record")
    List<Record> findAllRecord();

    /**
     * 根据id删除订单
     *
     * @param recordid 订单id
     * @return true
     */
    @Delete("delete from record where recordid=#{recordid}")
    Boolean deleteRecordById(Integer recordid);

    /**
     * 根据订单id查询订单信息
     *
     * @param recordid 订单id
     * @return Record
     */
    @Select("select * from record where recordid=#{recordid}")
    Record findRecordById(Integer recordid);

    /**
     * 根据订单id修改订单信息
     *
     * @param record 新的订单信息
     * @return true
     */
    @Update("update record set labId=#{labId},labname=#{labname},userid=#{userid},username=#{username},faculty=#{faculty},bookTime=#{bookTime} where recordid=#{recordid}")
    Boolean updateRecordById(Record record);

    /**
     * 把订单信息存入数据库
     *
     * @param record 订单信息
     * @return true
     */
    @Insert("insert into record (labid,labname,userid,username,userNum,faculty,bookTime,state) values (#{labid},#{labname},#{userid},#{username},#{userNum},#{faculty},#{bookTime},'未开始')")
    Boolean insertRecord(Record record);

    /**
     * 根据用户id查询用户预约记录
     *
     * @param userid:用户id
     * @return List<Record>
     */
    @Select("select * from record where userid=#{userid}")
    List<Record> findUserRecord(Integer userid);

    /**
     * 统计预约记录数量
     *
     * @return Integer
     */
    @Select("select count(*) from record")
    Integer countRecord();

    /**
     * 查询所有订单的预约时间
     *
     * @return List<String>
     */
    @Select("select bookTime from record")
    List<String> findAllRecordBookTime();

    /**
     * 查询未开始的预约订单
     *
     * @return List<String>
     */
    @Select("select recordid,bookTime from record where state='未开始'")
    List<Record> findSomeBookTime();

    /**
     * 根据订单id更新订单状态
     *
     * @param id 订单id
     * @return true
     */
    @Update("update record set state=#{state} where recordid=#{id}")
    Boolean updateStateById(Integer id, String state);

    /**
     * 根据实验室名称模糊查询订单信息
     *
     * @param labname 实验室名称
     * @return Record
     */
    @Select("select * from record where labname like '%${labname}%'")
    ArrayList<Record> queryRecordByLabName(String labname);
}
